// 延时跳转为了显示提示框
export async function delayNavigateTo(url: string, options?: any) {
    const delay = options?.delay || 1000;
    setTimeout(() => {
        navigateTo(url, options);
    }, delay);
}

// 判断是否tabBar使用不同的跳转api
export function _navigateTo(url: string, options?: any) {
    [
        '/pages/index/index',
        '/pages/category/index',
        '/pages/study/index',
        '/pages/mine/index',
    ].includes(url)
        ? uni.switchTab({url, ...options})
        : options?.isRedirect
            ? uni.redirectTo({url, ...options})
            : uni.navigateTo({url, ...options});
}

// 显示提示框、未登录返回
export default async function navigateTo(url: string, options?: any) {
    const {base} = useToast();
    const {isLogin} = useUser();
    if (options?.isNeedLogin && !isLogin) {
        base('请先登录');
        setTimeout(() => {
            navigateTo('/pages/login/captcha');
        }, 1000);
        return;
    }
    // 兼容浏览器使用页面跳转
    if (options?.isHistory) {
        // #ifdef H5
        history.pushState({}, '', url);
        // #endif
        // #ifndef H5
        _navigateTo(url, options);
        // #endif
    } else {
        _navigateTo(url, options);
    }
}

// 获取当前页面路由信息
export function getCurrentPage() {
    const pages = getCurrentPages();
    // @ts-expect-error-error
    return pages[pages.length - 1].$page as Page;
}

